

package com.net.advancedThread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


class Serice implements Runnable
{

  private final String name;
  private final int timeToStart;
  private final CountDownLatch latch;
  
  public Serice(String name,int timeToStart, CountDownLatch latch)
  {
	  this.name = name;
	  this.timeToStart = timeToStart;
	  this.latch = latch;
  }
	
	
	public void run()
	{
		try 
		{
			Thread.sleep(timeToStart);
			
		} catch (Exception e) 
		{
			// TODO: handle exception
		}
		
		System.out.println(name + "   is up");
		latch.countDown();
	}
	
}






public class CountDounLatchTest 
{


	
public static void main(String[] args)
	{
	  final CountDownLatch latch = new CountDownLatch(3);
	  
	  Thread cacheService = new Thread(new Serice("CacheService", 1000, latch));
	  Thread alertService = new Thread(new Serice("AlertService", 1000, latch));
	  Thread validationeService = new Thread(new Serice("ValidationService", 1000, latch));
		
		cacheService.start();
		alertService.start();
		validationeService.start();
		
		try {
			latch.await();
			System.out.println("all services are running now");
			
		} catch (Exception e) {
			// TODO: handle exception
		}
      
	}

}

